package com.nimt.hqgl_idea.service;


import com.nimt.hqgl_idea.util.ResponseBody;
import com.nimt.hqgl_idea.entity.Service;
import com.nimt.hqgl_idea.mapper.ServiceMapper;
import com.nimt.hqgl_idea.param.ServiceParam;
import com.nimt.hqgl_idea.util.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {
    @Autowired
    ServiceMapper serviceMapper;/*自动装配*/

    @Override
    public ResponseBody add(Service service) {
        ResponseBody responseBody = null;
        serviceMapper.insert(service);
        if(service.getId()!=null){
            responseBody = new ResponseBody(service);
        }else {
            responseBody = new ResponseBody(ErrorEnum.Service_Error);
        }
        return responseBody;
    }

    @Override
    public ResponseBody query(ServiceParam serviceParam) {
        ResponseBody responseBody = null;
        serviceParam.setTotalCount(serviceMapper.count(serviceParam.getUserId(),serviceParam.getKeywords()));
        int start = (serviceParam.getCurrentPage()-1)*serviceParam.getPageSize();
        List<Service> services = serviceMapper.selectAll(start, serviceParam.getPageSize(),serviceParam.getUserId(), serviceParam.getKeywords());
        if(services==null || services.size() == 0){
            responseBody = new ResponseBody(ErrorEnum.No_More_Data);
        }else {
            serviceParam.setData(services);
            responseBody = new ResponseBody(serviceParam);
        }
        return responseBody;
    }

    @Override
    public ResponseBody state(Service service) {
        return new ResponseBody(serviceMapper.update(service));
    }

    @Override
    public ResponseBody updateService(Service service) {
        ResponseBody responseBody = null;
        Service serviceOne = serviceMapper.selectById(service.getId());
        if (serviceOne != null){
            serviceMapper.update(service);
            responseBody = new ResponseBody(service);
        }else {
            responseBody = new ResponseBody("该报修记录不存在");
        }
        return responseBody;
    }

    @Override
    public ResponseBody delete(Integer id) {
        ResponseBody responseBody = null;
        if(serviceMapper.delete(id)>0){
            responseBody = new ResponseBody("删除成功!");
        }else {
            responseBody = new ResponseBody(ErrorEnum.Delete_Error);
        }
        return responseBody;
    }
}
